package com.eques.demo.lock

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.eques.base.BaseActivity
import com.eques.demo.R
import com.eques.demo.databinding.ActivityScreenTimeSettingBinding
import com.eques.https.ShaowUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONException
import org.json.JSONObject

class ScreenTimeSettingActivity : BaseActivity() {

    private lateinit var binding: ActivityScreenTimeSettingBinding
    private var selectedTime = 15 // 默认选择15秒
    private var devId = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityScreenTimeSettingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        devId = intent.getStringExtra("devId").toString()
        // 从Intent或SharedPreferences获取当前设置的时间
        val currentTime = intent.getIntExtra("current_screen_time", 15)
        selectedTime = currentTime

        initView()
    }

    override fun initView() {
        setupViews()
        setupClickListeners()
        updateSelectionUI()
    }

    override fun initData() {
    }

    private fun setupViews() {
        // 设置初始选择状态
        updateSelectionUI()
    }

    private fun setupClickListeners() {
        // 取消按钮
        binding.tvCancel.setOnClickListener {
            finish()
        }

        // 保存按钮
        binding.tvSave.setOnClickListener {
            saveScreenTimeSetting()
        }

        // 10秒选项
        binding.option10s.setOnClickListener {
            selectedTime = 10
            updateSelectionUI()
        }

        // 15秒选项
        binding.option15s.setOnClickListener {
            selectedTime = 15
            updateSelectionUI()
        }

        // 30秒选项
        binding.option30s.setOnClickListener {
            selectedTime = 30
            updateSelectionUI()
        }
    }

    private fun updateSelectionUI() {
        // 重置所有选择状态
        binding.ivCheck10s.visibility = ImageView.GONE
        binding.ivCheck15s.visibility = ImageView.GONE
        binding.ivCheck30s.visibility = ImageView.GONE

        // 根据选择显示对勾
        when (selectedTime) {
            10 -> binding.ivCheck10s.visibility = ImageView.VISIBLE
            15 -> binding.ivCheck15s.visibility = ImageView.VISIBLE
            30 -> binding.ivCheck30s.visibility = ImageView.VISIBLE
        }
    }

    private fun saveScreenTimeSetting() {
        // 保存设置到SharedPreferences
        val sharedPreferences = getSharedPreferences("screen_settings", MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putInt("screen_time", selectedTime)
        editor.apply()

        // 发送事件或回调通知设置已更新
        // EventBus.getDefault().post(ScreenTimeChangedEvent(selectedTime))

        // 显示保存成功提示
        Toast.makeText(this, "亮屏时间已设置为${selectedTime}秒", Toast.LENGTH_SHORT).show()
        updateShaowInfo();
        // 返回结果
        setResult(RESULT_OK)
        finish()
    }


    private fun updateShaowInfo(){
        CoroutineScope(Dispatchers.IO).launch {
            Log.e("更新影子设置数据", "开始")
            //VL2 影子设置
            val jsonObject = JSONObject()
            try {
                jsonObject.put("lcd_time",selectedTime.toString())
            } catch (e: JSONException) {
                Log.e("更新影子设置数据", e.message!!)
                e.printStackTrace()
            }
            Log.e("更新影子设置数据", jsonObject.toString())
            ShaowUtils.getInstance(context = this@ScreenTimeSettingActivity).updateSetting(devId, jsonObject)
        }
    }

    // 如果需要从其他地方获取当前设置
    companion object {
        fun getCurrentScreenTime(context: Context): Int {
            val sharedPreferences = context.getSharedPreferences("screen_settings", Context.MODE_PRIVATE)
            return sharedPreferences.getInt("screen_time", 15) // 默认15秒
        }
    }
}